<?php
class rchatModel extends appModel{
	/**
	 *获得一条微信record记录
	 */
	public function get_record($id)
	{
		if(!$id)
		{
			return false;
		}
		$sql = "SELECT *,nickname as name FROM ".$this->table('wxusers')." WHERE wxid =".$id;
		$result=$this->getRow($sql);
	
		if($result)
		{
	
			return $result;
		}
		else
		{
			return false;
		}
	}
	
	public function updateFrontTime($frontTime){
		if($frontTime>=15)
		{
			$sql = "UPDATE ".$this->table('options')." SET  op_value = 0 WHERE op_key='frontTime' ";
		}else{
			//记录自增
			$sql = " UPDATE ".$this->table('options')." SET  op_value = op_value+1 WHERE op_key='frontTime' ";
		}
		$this->query($sql);
	}
	
	public function get_special_ad(){//
		$time = yf_time();
		$sql = "SELECT title,url,pic_path,ad_starttime,ad_endtime FROM ".$this->table('block_content')." WHERE block_id = 21 AND  ad_endtime>= $time ORDER BY ad_endtime DESC ";
		$result = $this->getAll($sql);
		foreach ($result as $k=>$v)
		{
			$result[$k]['title'] = hy_dig_filter($v['title']);
		}
		return $result;
	}
	
	public function save_visitor($openid)
	{
		if($openid){
			$info['openid'] = $openid;
			$info['logintime'] = yf_time();
			$info['IP'] = hy_getIP();
			$this->insert($info, 'chat_visitor');
		}
	}
	public function get_comment_num($wxid)
	{
		if($wxid){
			$sql = "SELECT count(*) FROM ".$this->table('chat_record')." WHERE 	TO_DAYS(from_unixtime(addtime)) = TO_DAYS(NOW()) AND wxid=".$wxid;
			$count = $this->getOne($sql);
		}
		return $count;
	}
	
	public function get_special_chat($wxid,$num=0)
	{
		$time = yf_time();
		$allRes = array();
		$sql ="SELECT q2.showtime,q1.color,q1.pic_url,q1.group_id,q1.title,q1.source_url,q1.hongbao_id FROM (SELECT q3.special_id,q3.showtime FROM ".$this->table("chat_special_rule")." q3 WHERE q3.status = 1 )q2";
		$sql = $sql." LEFT JOIN ".$this->table("chat_special")." q1 ON q2.special_id = q1.id";
		$res = $this->getAll($sql);//秒数
		foreach ($res as $k=>$v)
		{
			$res[$k]['showTime'] = $time + $v['showtime'];
			$allRes[] = $res[$k];
		}
		
		$sql = "SELECT q1.color,q1.pic_url,q1.group_id,q1.title,q1.source_url,q1.hongbao_id FROM (SELECT q3.special_id FROM ".$this->table("chat_special_rule")." q3 WHERE q3.status = 2 AND comment_num<= $num )q2";
		$sql = $sql." LEFT JOIN ".$this->table("chat_special")." q1 ON q2.special_id = q1.id";
		$res2 = $this->getAll($sql);//条数
		foreach ($res2 as $k=>$v)
		{
			$res2[$k]['showTime'] = $time + rand(3, 15);
			
		}
		//每日
		$sql ="SELECT q2.specific_time,q1.color,q1.pic_url,q1.group_id,q1.title,q1.source_url,q2.time,q1.hongbao_id FROM (SELECT q3.special_id,q3.specific_time,q3.time FROM ".$this->table("chat_special_rule")." q3 WHERE q3.status = 3 )q2";
		$sql = $sql." LEFT JOIN ".$this->table("chat_special")." q1 ON q2.special_id = q1.id";
		$res3 = $this->getAll($sql);
		foreach ($res3 as $k=>$v){
			$hms =date("H:i:s", $res3[$k]['specific_time']);
			if(yf_time()<=strtotime(date( 'Y-m-d '.$hms, $time))){
				$res3[$k]['showTime']=strtotime(date( 'Y-m-d '.$hms, $time));
				$allRes[] = $res3[$k];
			}
		}
		//单日
		$sql ="SELECT q2.specific_time,q1.color,q1.pic_url,q1.group_id,q1.title,q1.source_url,q2.time,q1.hongbao_id  FROM (SELECT q3.special_id,q3.specific_time,q3.time FROM ".$this->table("chat_special_rule")." q3 WHERE q3.status = 4 AND q3.specific_time>=$time )q2";
		$sql = $sql." LEFT JOIN ".$this->table("chat_special")." q1 ON q2.special_id = q1.id";
		$res4 = $this->getAll($sql);
		foreach ($res4 as $k=>$v)
		{
			$res4[$k]['showTime'] =$v['specific_time'];
			$allRes[] = $res4[$k];
		}
		return $allRes;
	}
	
	public function create_record($info)
	{
		$result=array('error'=>0,'content'=>'吐槽失败');
		$time = $_SERVER['REQUEST_TIME'];
		if($info['wxid']>0)
		{
			$sql="SELECT count(*) FROM ".$this->table('chat_silence_user')." WHERE wxid=".$info['wxid']." AND endTime>=".$time;
			$num = $this->getOne($sql);
			if($num){
				$result['error'] = 4004;
				return $result;//该用户被禁言
			}
			$sql ="SELECT * FROM ".$this->table('chat_keyword');
			$keyword = $this->getAll($sql);
			foreach ($keyword as $k=>$v){
				if(stripos($info['comment'],$keyword[$k]['keyword']) === false){ //不包含
						
				}else{ //包含
					$result['error'] = 2002;
					return $result;//
				}
			}
		}
		else
		{
			$result['error'] = 3003;
			return $result;//失败
		}
	
		$field='';
		$value='';
		foreach($info as $k=>$v)
		{
			if($v!='')
			{
				$field.="$k,";
				$value.="'$v',";
	
			}
		}
	
		$field.=" addtime ";
		$value.=time();
		$sql="INSERT INTO ".$this->table('chat_record')." (".$field.") VALUES(".$value.")";
		$res=$this->query($sql);
		if($res)
		{
			//添加积分
// 			$chatIntegral = intval($this->get_cfg('chatIntegral'));
// 			$sql = "UPDATE ".$this->table('wxusers')." SET integral=integral+$chatIntegral WHERE wxid = ".$info['wxid']." LIMIT 1";
// 			$this->query($sql);
// 			$sql = "SELECT integral FROM ".$this->table('wxusers')." WHERE wxid = ".$info['wxid']." LIMIT 1 ";
// 			$integral = $this->getOne($sql);
			$result['error'] = 1;
			$result['content'] = '';
// 			$result['integral'] = $integral;
// 			$endTime = strtotime(date( 'Y-m-d 23:59:59',yf_time()));
// 			$startTime = strtotime(date( 'Y-m-d', yf_time()));
// 			$sql = "SELECT COUNT(*) FROM ".$this->table('chat_record')." WHERE wxid = ".$info['wxid']." AND addtime>=$startTime AND addtime<=$endTime";
// 			$num = $this->get_comment_num($info['wxid']);
// 			$sql = "SELECT * FROM (SELECT q3.special_id FROM ".$this->table("chat_special_rule")." q3 WHERE q3.status = 2 AND comment_num= $num )q2";
// 			$sql = $sql." LEFT JOIN ".$this->table("chat_special")." q1 ON q2.special_id = q1.id";
// 			$res2 = $this->getAll($sql);
// 			if($res2){
// 				$result['content'] = $res2;
// 			}
		}
		return $result;
	}
	
	public function get_now_hongbaoId(){
		$title = date('md',time())."-首页红包";
		$sql = "SELECT id FROM ".$this->table('lucky_info')." WHERE title='".$title."' and status =1 LIMIT 1";
		$res = $this->getOne($sql);
		if($res){
			return $res;
		}else{
			return 0;
		}
// 		return $this->getOne($sql);
	}
	
	public function get_new_hongbaoId(){
	    $title = date('md',time())."首页红包";
	    if($title=="0207首页红包"){
	        $title=="02071首页红包";
	    }
	    $sql = "SELECT id FROM ".$this->table('lucky_info')." WHERE title='".$title."' and status =1 LIMIT 1";
	    $res = $this->getOne($sql);
	    if($res){
	        return $res;
	    }else{
	        return 0;
	    }
	    // 		return $this->getOne($sql);
	}
	
	public function get_luckyDraw($id){
		$sql = "SELECT lottery_id FROM ".$this->table('lucky_info')." WHERE id = $id";
		$result =  $this->getOne($sql);
		return $result;
	}
	
	public function getHomeInfo()
	{
		//获取banner
		$sql = "SELECT url,pic_path FROM ".$this->table('block_content')." WHERE block_id = 22  ORDER BY ad_endtime DESC ,ordering DESC ";
		$result['homeBanner'] = $this->getAll($sql);
		//新闻延伸
		$sql = "SELECT url,pic_path FROM ".$this->table('block_content')." WHERE id = 72  LIMIT 1 ";
		$result['news'] = $this->getRow($sql);
		//四个方法的路径
		$sql = "SELECT id,url FROM ".$this->table('block_content')." WHERE id in (68,69,70,71)  ";
		$result['four'] = $this->getAll($sql);
		//活动弹层
		$sql = "SELECT pic_path FROM ".$this->table('block_content')." WHERE id = 75  LIMIT 1 ";
		$result['activity'] = $this->getOne($sql);
		return $result;
	}
}